package com.gardenguell.salad.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.gardenguell.salad.R;
import com.gardenguell.salad.data.model.Box;

import java.util.Collections;
import java.util.List;

/**
 * Created by absurd on 16/2/10.
 */
public class BoxAdapter extends ClickableRecyclerViewAdapter<RecyclerView.ViewHolder>{

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    private List<Box> mData = Collections.EMPTY_LIST;

    public BoxAdapter(List<Box> data){
        if(data != null)
            this.mData  = data;
    }

    public void setData(List<Box> data){
        this.mData = data;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0)
            return TYPE_HEADER;
        return TYPE_ITEM;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_HEADER){
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_box_header, parent, false);
            return new HeaderViewHolder(view);
        }else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_box, parent, false);
            // add click listener for view
            view.setOnClickListener(this);
            view.setOnLongClickListener(this);
            return new ItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ItemViewHolder) {
            Box bd = mData.get(position - 1);
            ItemViewHolder itemViewHolder = (ItemViewHolder)holder;
            if (position == 0) {
                itemViewHolder.boxImage.setImageResource(R.mipmap.box_image1);
            } else {
                itemViewHolder.boxImage.setImageResource(R.mipmap.box_image2);
            }
            holder.itemView.setTag(bd);
        }
    }

    @Override
    public int getItemCount() {
        return mData.size() + 1;
    }

    public static class HeaderViewHolder extends RecyclerView.ViewHolder{
        public ImageView xhoogeeLogo;
        public HeaderViewHolder(View view){
            super(view);
            xhoogeeLogo = (ImageView)view.findViewById(R.id.xhoogee_logo);
        }
    }


    public static class ItemViewHolder extends RecyclerView.ViewHolder{
        public ImageView boxImage;
        public TextView boxName;
        public ItemViewHolder(View view){
            super(view);
            boxImage = (ImageView) view.findViewById(R.id.box_image);
            boxName = (TextView)view.findViewById(R.id.box_name);
        }

    }


}
